package com.li.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.li.educenter.entity.UcenterMember;
import com.li.educenter.entity.vo.RegisterVo;
import com.li.educenter.mapper.UcenterMemberMapper;
import com.li.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.li.servicebase.exceptionhandler.XhuihuiException;
import com.li.util.JwtUtils;
import com.li.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-04-15
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    //登录
    @Override
    public String login(UcenterMember member) {
        String mobile = member.getMobile();
        String password = member.getPassword();

        //判断手机号和密码是否为空
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new XhuihuiException(20001,"登录失败");
        }
        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if (mobileMember == null){
            throw new XhuihuiException(20001,"手机号不存在");
        }

        //判断密码
        //因为存储到数据库密码肯定是加密的
        //把输入的密码进行加密，再和数据库密码进行比较
        //加密方式MD5

        if (!MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw new XhuihuiException(20001,"密码不存在");
        }

        //判断用户是否被禁用
        if (mobileMember.getIsDisabled()){
            throw new XhuihuiException(20001,"用户不存在");
        }

        //登录成功 之后返回json字符串,使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());

        return jwtToken;
    }

    //注册
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册的数据
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        String mobile = registerVo.getMobile();
        String code = registerVo.getCode();
        //非空判断
        if (StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(mobile) ||StringUtils.isEmpty(code)){
            throw new XhuihuiException(20001,"注册失败");
        }
        //判断验证码
        //获取redis验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)){
            throw new XhuihuiException(20001,"验证码错误");
        }
        //判断手机号是否重复，表里面存在相同的手机号不能进行注册添加
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0){
            throw new XhuihuiException(20001,"手机号已注册");
        }

        //数据添加数据库中
        UcenterMember member = new UcenterMember();
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setMobile(mobile);
        member.setIsDisabled(false);
        member.setAvatar("https://xhuihui.oss-cn-shanghai.aliyuncs.com/2022/04/05/88875069790144ea98835dace4031ecbfile.png");
        baseMapper.insert(member);
    }

    //根据openid判断
    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    //擦和讯某一天注册人数
    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
